package tech.tongyu.bob.demo.proxy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.tongyu.bob.demo.proxy.jdkproxy.proxy.TransactionProxy;
import tech.tongyu.bob.demo.proxy.jdkproxy.service.UserService;
import tech.tongyu.bob.demo.proxy.jdkproxy.service.impl.UserServiceImpl;

import javax.annotation.Resource;
import java.lang.reflect.Proxy;

@RestController
public class UserController {


    private  UserService userService ;

    @Autowired
    public UserController(UserServiceImpl userServiceImpl) {
        this.userService = TransactionProxy.createProxy(userServiceImpl);
    }

    @GetMapping("/jdkproxy/{username}/{email}")
    public ResponseEntity<String> registerUser(@PathVariable String username, @PathVariable String email) {
        userService = TransactionProxy.createProxy(userService);
        // 调用代理类的 register
        userService.registerUser(username, email);
        return ResponseEntity.ok("register success");
    }

    @GetMapping("/jdkproxy")
    public ResponseEntity<String> login(@RequestParam String username) {
        // 调用代理类的 register
        UserService userProxy = TransactionProxy.createProxy(userService);
        userProxy.login(username);
        return ResponseEntity.ok("login success");
    }

    @GetMapping("/internalMethod")
    public ResponseEntity<String> internalMethod() {
        // 调用代理类
        userService.internalMethod();
        return ResponseEntity.ok("login success");
    }
}
